#include<iostream>
#include<vector>
#include<string>
#include<chrono>
#include<set>
#include<sw/redis++/redis++.h>
#include<thread>
#include"util.hpp"
using sw::redis::Redis;
using namespace std::chrono_literals;
using namespace std;

void test1(Redis& redis)
{
    cout<<"zadd and zrange"<<endl;
    redis.flushall();

    redis.zadd("key","吕布",99.5);

    redis.zadd("key",{
        make_pair("赵云",98),
        make_pair("董卓",78),
        make_pair("刘备",68),
    });

    vector<pair<string,double>> member = {
        make_pair("张飞",93),
        make_pair("关羽",99)
    };
    redis.zadd("key",member.begin(),member.end());

    // zrange 支持两种主要的风格
    // 1. 只查询member，不带score
    // 2. 查询member，同时带score
    // 关键看插入迭代器 指向的容器的类型 
    // 如果容器的类型只包含一个string 则为1
    // 如果是pair，且有string和double，则为2

    vector<string> members;
    auto it = std::back_inserter(members);
    redis.zrange("key",0,-1,it);
    printContainer(members);

    vector<pair<string,double>> members_withscores;
    auto it2 = std::back_inserter(members_withscores);
    redis.zrange("key",0,-1,it2);
    printContainerPair(members_withscores);

}

void test2(Redis& redis)
{

    cout<<"zcard 获取元素个数"<<endl;
    redis.flushall();

    redis.zadd("key","吕布",99.5);
    redis.zadd("key","wenjiaji",96);
    redis.zadd("key","sad",96.5);
    redis.zadd("key","qeq",96.5);

    long long ret = redis.zcard("key");
    cout<<ret<<endl;
}

void test3(Redis& redis)
{

    cout<<"zrem 删除指定元素"<<endl;
    redis.flushall();

    redis.zadd("key","吕布",99.5);
    redis.zadd("key","wenjiaji",96);
    redis.zadd("key","sad",96.5);
    redis.zadd("key","qeq",96.5);

    redis.zrem("key","吕布");

    long long ret = redis.zcard("key");
    cout<<ret<<endl;
}


void test4(Redis& redis)
{
    cout<<"zscore 查询分数"<<endl;
    redis.flushall();

    redis.zadd("key","吕布",99.5);
    redis.zadd("key","wenjiaji",96);
    redis.zadd("key","sad",96.5);
    redis.zadd("key","qeq",96.5);

    auto it = redis.zscore("key","吕布"); // optional double
    if(it)
        cout<<it.value()<<endl;
    else
        cout<<"查无此人"<<endl;
}

void test5(Redis& redis)
{
    cout<<"zrank 查询某人的排名 "<<endl;
    redis.flushall();

    redis.zadd("key","吕布",99.5);
    redis.zadd("key","wenjiaji",96);
    redis.zadd("key","sad",96.5);
    redis.zadd("key","qeq",96.5);

    auto ret = redis.zrank("key","qeq");
    if(ret)
        cout<<ret.value()<<endl;
    else
        cout<<"查无此人"<<endl;

}


int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    test5(redis);
}